package com.icongyou.enterprise.talent_marking.service.impl;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.data_aggregation.mapper.DataAggregationMapper;
import com.icongyou.enterprise.talent_marking.entity.PerformanceChanges;
import com.icongyou.enterprise.talent_marking.entity.vo.ChangeDetailVO;
import com.icongyou.enterprise.talent_marking.entity.vo.ChangeItemVO;
import com.icongyou.enterprise.talent_marking.mapper.PerformanceChangeMapper;
import com.icongyou.enterprise.talent_marking.service.PerformanceChangesService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PerformanceChangesServiceImpl implements PerformanceChangesService {
    @Resource
    private PerformanceChangeMapper changesMapper;
    @Resource
    private DataAggregationMapper dataAggregationMapper;
    @Override
    public Result<List<ChangeDetailVO>> getRecentChanges() {
        // 获取最近7天的变化
        LocalDate sinceDate = LocalDate.now().minusDays(7);
        // 直接测试查询
        List<PerformanceChanges> changes = changesMapper.selectRecentChanges(sinceDate);

        if (changes.isEmpty()) {
        } else {
            PerformanceChanges first = changes.get(0);

            // 手动检查对象的所有字段
        }

        // 按学生分组
        Map<String, ChangeDetailVO> groupedChanges = changes.stream()
                .collect(Collectors.toMap(
                        change -> change.getStudentId(),
                        change -> {
                            ChangeDetailVO vo = new ChangeDetailVO();
                            vo.setStudentId(change.getStudentId());
                            vo.setStudentName(getStudentName(change.getStudentId()));

                            vo.setChanges(new ArrayList<>());
                            vo.getChanges().add(createChangeItem(change));
                            vo.setLatestChangeTime(change.getDetectedTime());
                            return vo;
                        },
                        (existing, newVo) -> {
                            existing.getChanges().addAll(newVo.getChanges());
                            return existing;
                        }
                ));

        List<ChangeDetailVO> result = new ArrayList<>(groupedChanges.values());
        return Result.success("发现" + result.size() + "个课程的变化", result);
    }

    private ChangeItemVO createChangeItem(PerformanceChanges change) {
        ChangeItemVO item = new ChangeItemVO();
        item.setCourseId(change.getCourseId());
        item.setCourseName(change.getCourseName());
        item.setChangeType(change.getChangeType());
        item.setChangeDescription(change.getChangeDescription());
        item.setDetectedTime(change.getDetectedTime());
        return item;
    }

    private String getStudentName(String studentId) {
        try {
            return dataAggregationMapper.selectRealNameById(studentId);
        } catch (Exception e) {
            return "未知学生";
        }
    }
}
